package fr.insta.reverse.uml.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import fr.insta.reverse.uml.bean.Projet;
import fr.insta.reverse.uml.bean.User;
import fr.insta.reverse.uml.dao.DaoFactory;
import fr.insta.reverse.uml.dao.DaoName;
import fr.insta.reverse.uml.dao.DaoUser;
import fr.insta.reverse.uml.util.InstaUtils;

public class ServiceUser implements IService<User>
{
	DaoUser dao = (DaoUser) DaoFactory.getDao(DaoName.User);

	@Override
	public User get(int _id)
	{
		return dao.findById(_id);
	}

	@Override
	public List<User> getObjects()
	{
		return dao.getAll();
	}

	@Override
	public User insert(User _object)
	{
		return dao.save(_object);
	}

	@Override
	public User update(User _object)
	{
		return dao.save(_object);
	}

	public User connect(User u)
	{
		return dao.getByUserAndPass(u);
	}

	public User findByMail(User u)
	{
		return dao.getByMail(u);
	}

	@Override
	public boolean delete(User _object)
	{
		return dao.remove(_object);
	}

	public String getErrorMessageForNewUser(User _user)
	{
		String errorMessage = null;

		if (findByMail(_user) != null)
			errorMessage = "Le mail existe déjà.";

		if (_user.getPassword() == null || !_user.getPassword().contentEquals(_user.getPasswordConfirm()))
			errorMessage = "Les mots de passes ne correspondent pas.";

		if (_user.getPasswordConfirm() == null || _user.getPasswordConfirm().isEmpty())
			errorMessage = "Veuillez confirmer votre mot de passe.";

		if (_user.getPassword() == null || _user.getPassword().length() < 5)
			errorMessage = "Le mot de passe est obligatoire et doit faire au moins 5 caractères.";

		if (_user.getMail() == null || !InstaUtils.isEmail(_user.getMail()))
			errorMessage = "L'email est obligatoire !";

		if (_user.getPrenom() == null || _user.getPrenom().length() < 2)
			errorMessage = "Le prenom est obligatoire !";

		if (_user.getNom() == null || _user.getNom().length() < 2)
			errorMessage = "Le nom est obligatoire !";

		return errorMessage;
	}

	public String getErrorMessageForConnection(User _user)
	{
		String errorMessage = null;

		if (_user.getPassword() == null || _user.getPassword().length() < 5)
			errorMessage = "Le mot de passe est obligatoire et doit faire au moins 5 caractères.";

		if (_user.getMail() == null || !InstaUtils.isEmail(_user.getMail()))
			errorMessage = "L'email est obligatoire !";

		return errorMessage;
	}

	public boolean isAuthorizedToView(String _idProjectParam, User _user)
	{
		ServiceProject serviceProjet = (ServiceProject) ServiceFactory.getService(ServiceName.ServiceProject);

		try
		{
			int idProject = Integer.parseInt(_idProjectParam);

			Projet project = serviceProjet.get(idProject);

			//If the project cannot be found, return false
			if (project == null)
				return false;
			else
			{
				//Return true is project is public
				if (project.getVisibilite().getLib().contentEquals("Publique"))
				{
					return true;
				}
				else
				{
					System.out.println("check prop");
					if (_user == null)
					{
						return false;
					}
					//check if the user is the owner of the project
					else if (project.getProp().getId() == _user.getId())
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
		catch (Exception _e)
		{
			return false;
		}
	}

	public void logoutUser(HttpServletRequest _request)
	{
		_request.getSession().setAttribute("User", null);
	}

	public User getSessionUser(HttpServletRequest _request)
	{
		return (User) _request.getSession().getAttribute("User");
	}

	public void setSessionUser(HttpServletRequest _request, User _user)
	{

		_request.getSession().setAttribute("User", _user);
	}
}